function _G.get_oil_winbar()
  local dir = require("oil").get_current_dir()
  if dir then
    return vim.fn.fnamemodify(dir, ":~")
  else
    return vim.api.nvim_buf_get_name(0)
  end
end
local detail = false

return {
  {
    -- ranger
    "kelly-lin/ranger.nvim",
    enabled = true,
    config = function()
      require("ranger-nvim").setup({ replace_netrw = true })
      vim.api.nvim_set_keymap("n", "R", "", {
        noremap = true,
        callback = function()
          require("ranger-nvim").open(true)
        end,
      })
    end,
  },
  {
    -- oil
    "stevearc/oil.nvim",
    lazy = true,
    enabled = true,
    keys = {
      { "-", "<cmd>Oil<cr>", desc = "Open parent directory" },
    },
    dependencies = { "nvim-mini/mini.icons" },
    opts = {
      default_file_explorer = true, -- Use Oil.nvim as the default file explorer
      view_options = {
        show_hidden = true, -- Show hidden files
      },
      float = {
        padding = 5, -- Add padding to the floating window
      },
    },
    config = function()
      require("oil").setup({
        columns = {
          "icon",
          "permissions",
          "size",
          "mtime",
        },
        -- Set to false to disable all of the above keymaps
        use_default_keymaps = false,
        keymaps = {
          ["?"] = { "actions.show_help", mode = "n" },
          ["<CR>"] = "actions.select",
          ["<C-l>"] = { "actions.select", opts = { vertical = true } },
          ["<C-j>"] = { "actions.select", opts = { horizontal = true } },
          ["<C-t>"] = { "actions.select", opts = { tab = true } },
          ["<C-p>"] = "actions.preview",
          ["<C-c>"] = { "actions.close", mode = "n" },
          ["<C-r>"] = "actions.refresh",
          ["<bs>"] = { "actions.parent", mode = "n" },
          ["-"] = { "actions.open_cwd", mode = "n" },
          ["`"] = { "actions.cd", mode = "n" },
          ["~"] = { "actions.cd", opts = { scope = "tab" }, mode = "n" },
          ["gs"] = { "actions.change_sort", mode = "n" },
          ["gx"] = "actions.open_external",
          ["<c-h>"] = { "actions.toggle_hidden", mode = "n" },
          ["g\\"] = { "actions.toggle_trash", mode = "n" },
          ["gd"] = {
            desc = "Toggle file detail view",
            callback = function()
              detail = not detail
              if detail then
                require("oil").set_columns({ "icon", "permissions", "size", "mtime" })
              else
                require("oil").set_columns({ "icon" })
              end
            end,
          },
        },
        view_options = {
          -- Show files and directories that start with "."
          show_hidden = false,
          -- This function defines what is considered a "hidden" file
          is_hidden_file = function(name, bufnr)
            local m = name:match("^%.")
            return m ~= nil
          end,
          -- This function defines what will never be shown, even when `show_hidden` is set
          is_always_hidden = function(name, bufnr)
            return false
          end,
          -- Sort file names with numbers in a more intuitive order for humans.
          -- Can be "fast", true, or false. "fast" will turn it off for large directories.
          natural_order = "fast",
          -- Sort file and directory names case insensitive
          case_insensitive = false,
          sort = {
            -- sort order can be "asc" or "desc"
            -- see :help oil-columns to see which columns are sortable
            { "type", "asc" },
            { "name", "asc" },
          },
          -- Customize the highlight group for the file name
          highlight_filename = function(entry, is_hidden, is_link_target, is_link_orphan)
            return nil
          end,
        },
        -- Configuration for the file preview window
        preview_win = {
          -- Whether the preview window is automatically updated when the cursor is moved
          update_on_cursor_moved = true,
          -- How to open the preview window "load"|"scratch"|"fast_scratch"
          preview_method = "fast_scratch",
          -- A function that returns true to disable preview on a file e.g. to avoid lag
          disable_preview = function(filename)
            return false
          end,
          -- Window-local options to use for preview window buffers
          win_options = {},
        },
        win_options = {
          -- Show CWD in the winbar
          winbar = "%!v:lua.get_oil_winbar()",
          -- Change the oil configuration to allow at least 1 sign columns
          signcolumn = "yes:1",
        },
      })
    end,
  },
  {
    -- Shows git status of files in statuscolumn
    "refractalize/oil-git-status.nvim",
    dependencies = {
      "stevearc/oil.nvim",
    },
    config = true,
    config = function()
      require("oil-git-status").setup({
        show_ignored = true, -- show files that match gitignore with !!
        symbols = { -- customize the symbols that appear in the git status columns
          index = {
            ["!"] = "!",
            ["?"] = "?",
            ["+"] = "A",
            ["C"] = "C",
            ["-"] = "D",
            ["M"] = "M",
            ["R"] = "R",
            ["T"] = "T",
            ["U"] = "U",
            [" "] = " ",
          },
          working_tree = {
            ["!"] = "!",
            ["?"] = "?",
            ["+"] = "A",
            ["C"] = "C",
            ["-"] = "D",
            ["M"] = "M",
            ["R"] = "R",
            ["T"] = "T",
            ["U"] = "U",
            [" "] = " ",
          },
        },
      })
    end,
  },
}
